﻿using System;
using System.Drawing;
using System.Linq;

using Spencen.Mobile.Markup;
using Spencen.Mobile.UI;
using Spencen.Mobile.UI.Animations;
using Spencen.Mobile.UI.Commanding;
using Spencen.Mobile.UI.Controls;
using Spencen.Mobile.UI.Easings;
using Spencen.Mobile.UI.Primitives;
using Spencen.Mobile.UI.Transforms;
using Spencen.Mobile.UI.Navigation;

namespace Demo.Presenters
{
    public class PrimitivesPresenter : ParentedPresenter
    {
        private DrawingContainer _container;

        public PrimitivesPresenter( INavigator navigator ) : base( navigator )
        {
            RandomizeCommand = new DelegateCommand( Randomize );
        }

        public ICommand RandomizeCommand { get; private set; }

        public override View CreateView( IDrawingHost host )
        {
            // XAML Loading
            var text = (System.IO.Stream)System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( "Demo.Views.PrimitivesView.xml" );
            var xamlRenderer = new XamlRenderer( host, this );
            var view = (View)xamlRenderer.Render( text );

            _container = (DrawingContainer)xamlRenderer.FindResource( "container" );

            return view;
        }

        private void Randomize()
        {
            var random = new Random();

            foreach ( var childElement in _container.Children )
            {
                // Add a ScaleTransform if we don't have one already.
                if ( !childElement.Transforms.OfType<ScaleTransform>().Any() )
                    childElement.Transforms.Add( new ScaleTransform() );

                var animationDuration = new TimeSpan( 0, 0, 0, 0, random.Next( 500 ) + 500 );
                var colorAnimation = new BrushAnimation( childElement, childElement is TextElement ? "Foreground" : "Background", animationDuration )
                {
                    FinalValue = new SolidBrush( Color.FromArgb( random.Next(256), random.Next(256), random.Next(256) ) ),
                    EasingFunction = GetEasingFunction()
                };
                var scaleXAnimation = new FloatAnimation( childElement.Transforms[ 0 ], "ScaleX", animationDuration )
                {
                    FinalValue = random.Next( 20 ) / 10.0f + 1.0f,
                    EasingFunction = GetEasingFunction()
                };
                var scaleYAnimation = new FloatAnimation( childElement.Transforms[ 0 ], "ScaleY", animationDuration )
                {
                    FinalValue = scaleXAnimation.FinalValue,
                    EasingFunction = scaleXAnimation.EasingFunction
                };
                var movementAnimation = new PointAnimation( childElement, "Center", animationDuration ) 
                { 
                    FinalValue = new Point( random.Next( _container.Size.Width), random.Next( _container.Size.Height ) ),
                    EasingFunction = GetEasingFunction()
                };
                var rotateAnimation = new FloatAnimation( childElement, "Angle", animationDuration ) 
                { 
                    FinalValue = random.Next( 360 ),
                    EasingFunction = GetEasingFunction()
                };

                _container.Host.AnimationManager.AddAnimation( colorAnimation, false );
                _container.Host.AnimationManager.AddAnimation( scaleXAnimation, false );
                _container.Host.AnimationManager.AddAnimation( scaleYAnimation, false );
                _container.Host.AnimationManager.AddAnimation( movementAnimation, false );
                _container.Host.AnimationManager.AddAnimation( rotateAnimation, false );
            }
        }

        private IEasingFunction GetEasingFunction()
        {
            return new SineWaveEasing();
        }
    }
}
